package UI;

import controller.book.Book_add;
import controller.book.Book_update;
import entity.Book;
import entity.User;
import util.JdbcDeal;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Vector;

public class UIbook extends JPanel implements ActionListener {
    User user;
    JPanel p1,p2;
    JComboBox jComboBox;
    JTextField jTextField;
    String[][] obj;//表格数据
    JTable table;
    JScrollPane scrollpane; //表格面板
    String arr[];

    JButton jButton_select;     //查询
    JButton jButton_remove;     //删除
    JButton jButton_update;     //更新
    JButton jButton_add;     //添加
    JButton jButton_all;     //显示全部

    ResultSet resultSet;
    JdbcDeal jdbcDeal;
    int Count;        //获取信息行数
    JComboBox jComboBox_sort;    //分类管理
    Vector<String> vector = new Vector<String>();   //存放类别信息
    private HashMap hashMap = new HashMap();

    private void UpdateTable(){
        p2.remove(scrollpane);
        table=new JTable(obj,arr);
        table.setRowHeight(50);
        scrollpane=new JScrollPane(table);
        p2.add(scrollpane, BorderLayout.CENTER);
        p2.revalidate();
        this.revalidate();
    }

    private void sort_select(){
        try {
            vector.add("");
            String sql = "select * from sort";
            jdbcDeal = new JdbcDeal();
            resultSet = jdbcDeal.select(sql);
            while (resultSet.next()){
                vector.add(resultSet.getString("Sname"));
                hashMap.put(resultSet.getString("Sname"),resultSet.getString("Sno"));
            }
            jdbcDeal.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    private boolean Table_select(String sql){
        //初始化数据
        try {
            jdbcDeal = new JdbcDeal();
            resultSet = jdbcDeal.select(sql);
            resultSet.last();
            Count = resultSet.getRow();
            resultSet.beforeFirst();
            obj = new String[Count][arr.length];
            int i=0;
            if(!resultSet.next()){
                jdbcDeal.close();
                JOptionPane.showMessageDialog(this, "未查询到相关信息", "", JOptionPane.WARNING_MESSAGE);
                return false;
            }else{
                do{
                    obj[i][0] = resultSet.getString("Bno");
                    obj[i][1] = resultSet.getString("Wname");
                    obj[i][2] = resultSet.getString("Cname");
                    obj[i][3] = resultSet.getString("Sname");
                    obj[i][4] = resultSet.getString("Gname");
                    obj[i][5] = resultSet.getString("Bnum");
                    obj[i][6] = resultSet.getString("Bdate");
                    obj[i][7] = resultSet.getString("cash");
                    i++;
                }while (resultSet.next());
                jdbcDeal.close();
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return true;
    }

    public UIbook(User user) {
        this.user = user;

        p1 = new JPanel();
        p1.setOpaque(false);
        Box boxH = Box.createHorizontalBox(); //行式盒
        jButton_all = new JButton("重置");
        JLabel label = new JLabel("请输入查询信息：");
        jTextField = new JTextField(15);
        jComboBox = new JComboBox(new String[]{"","编号","销售人员","客户","货物","日期"});
        jButton_select = new JButton("查询");
        jButton_add = new JButton("添加");
        jButton_remove = new JButton("删除");
        jButton_update = new JButton("修改");

        JLabel jLabel_sort = new JLabel("商品类别");
        sort_select();
        jComboBox_sort = new JComboBox(vector);

        boxH.add(jLabel_sort);
        boxH.add(Box.createHorizontalStrut(10));
        boxH.add(jComboBox_sort);
        boxH.add(Box.createHorizontalStrut(80));
        boxH.add(jButton_all);
        boxH.add(Box.createHorizontalStrut(10));
        boxH.add(label);
        boxH.add(jTextField);
        boxH.add(jComboBox);
        boxH.add(jButton_select);
        boxH.add(jButton_add);
        boxH.add(jButton_update);
        boxH.add(jButton_remove);
        p1.add(boxH);

        arr = new String[]{"编 号","销售人员","客 户","类 别","货 物","数 量","日 期","价 钱"};
        String sql = "SELECT * FROM v_book";
        Table_select(sql);

        this.setLayout(new BorderLayout());
        table=new JTable(obj,arr);
        //表格限宽
        table.setRowHeight(50);
        //创建显示表格的滚动面板
        scrollpane=new JScrollPane(table);

        p2 = new JPanel();
        p2.setLayout(new BorderLayout());
        p2.add(scrollpane,BorderLayout.CENTER);
        p2.add(new JButton("*********订单信息**********"),BorderLayout.NORTH);
        p2.add(new JButton("-------------------------"),BorderLayout.SOUTH);
        p2.setOpaque(false);

        Box boxV = Box.createVerticalBox();
        boxV.add(p1);
        boxV.add(p2);
        jButton_all.addActionListener(this);
        jButton_select.addActionListener(this);
        jButton_remove.addActionListener(this);
        jButton_update.addActionListener(this);
        jButton_add.addActionListener(this);
        jComboBox_sort.addActionListener(this);

        this.setLayout(new BorderLayout());
        this.add(boxV,BorderLayout.CENTER);
        setVisible(true);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String texts = jTextField.getText();
        //下拉框
        String jBox = jComboBox.getSelectedItem().toString();
        String jBox_sort = jComboBox_sort.getSelectedItem().toString();

        if (e.getSource() == jButton_select) {
            boolean select_flag = false;
            if (texts.equals("")) {
                JOptionPane.showMessageDialog(this, "请输入查询信息!", "", JOptionPane.WARNING_MESSAGE);
                return;
            } else if (jBox.equals("")) {
                JOptionPane.showMessageDialog(this, "请选择查找类型!", "", JOptionPane.WARNING_MESSAGE);
                return;
            } else if (jBox.equals("货物")) {
                String sql = "SELECT * FROM v_book WHERE Gname = '" + texts + "'";
                if(!jBox_sort.equals("")){
                    sql = "SELECT * FROM v_book WHERE Sname = '" + jBox_sort + "' AND Gname like '" + texts + "'";
                }
                select_flag = Table_select(sql);
            } else if (jBox.equals("日期")) {
                String sql = "SELECT * FROM v_book WHERE Bdate = '" + texts + "'";
                if(!jBox_sort.equals("")){
                    sql = "SELECT * FROM v_book WHERE Sname = '" + jBox_sort + "' AND Bdate = '" + texts + "'";
                }
                select_flag = Table_select(sql);
            } else if (jBox.equals("编号")) {
                String sql = "SELECT * FROM v_book WHERE Bno = '" + texts + "'";
                select_flag = Table_select(sql);
            } else if (jBox.equals("销售人员")) {
                String sql = "SELECT * FROM v_book WHERE Wname = '" + texts + "'";
                if(!jBox_sort.equals("")){
                    sql = "SELECT * FROM v_book WHERE Sname = '" + jBox_sort + "' AND Wname = '" + texts + "'";
                }
                select_flag = Table_select(sql);
            } else if (jBox.equals("客户")) {
                String sql = "SELECT * FROM v_book WHERE Cname = '" + texts + "'";
                if(!jBox_sort.equals("")){
                    sql = "SELECT * FROM v_book WHERE Sname = '" + jBox_sort + "' AND Cname = '" + texts + "'";
                }
                select_flag = Table_select(sql);
            }
            if (select_flag) {
                p2.remove(scrollpane);
                UpdateTable();
                return;
            }

        } else if (e.getSource() == jButton_add) {
            Book_add book_add = new Book_add(user);
            String sql = "SELECT * FROM v_book";
            Table_select(sql);
            UpdateTable();
            return;
        } else if (e.getSource() == jButton_all) {
            String sql = "SELECT * FROM v_book";
            Table_select(sql);
            UpdateTable();
            return;
        } else if (e.getSource() == jButton_remove || e.getSource() == jButton_update) {
            if (table.getSelectedRows().length > 1 || table.getSelectedRow() == -1) {
                JOptionPane.showMessageDialog(this, "请选择一行！", "", JOptionPane.WARNING_MESSAGE);
                return;
            } else if (user.isFlag() || user.getName().equals((String) table.getValueAt(table.getSelectedRow(), 1))) {
                Book book = new Book();
                book.setBookNo((String) table.getValueAt(table.getSelectedRow(), 0));
                book.setWno((String) table.getValueAt(table.getSelectedRow(), 1));
                book.setCno((String) table.getValueAt(table.getSelectedRow(), 2));
                book.setGno((String) table.getValueAt(table.getSelectedRow(), 4));
                book.setBnum(Integer.valueOf((String) table.getValueAt(table.getSelectedRow(), 5)));
                book.setBdate((String) table.getValueAt(table.getSelectedRow(), 6));
                if (e.getSource() == jButton_remove) {
                    jdbcDeal = new JdbcDeal();
                    try {
                        if (jdbcDeal.Bookdel(book) == 1) {
                            JOptionPane.showMessageDialog(this, "成功删除一条记录！", "", JOptionPane.PLAIN_MESSAGE);
                            String sql = "SELECT * FROM v_book";
                            Table_select(sql);
                            UpdateTable();
                            return;
                        } else {
                            JOptionPane.showMessageDialog(this, "删除记录失败！可能和其他信息有联动！请先删除相关信息！", "", JOptionPane.WARNING_MESSAGE);
                            return;
                        }
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                } else if (e.getSource() == jButton_update) {
                    Book_update book_update = new Book_update(book);
                    String sql = "SELECT * FROM v_book";
                    Table_select(sql);
                    UpdateTable();
                    return;
                }
            } else {
                JOptionPane.showMessageDialog(this, "管理员或销售人员为本人才能进行操作！", "", JOptionPane.WARNING_MESSAGE);
                return;
            }
        } else {
            if (!jBox_sort.equals("")) {
                String sql = "SELECT * FROM v_book WHERE Sname = '" + jBox_sort +"'";
                Table_select(sql);
                UpdateTable();
                return;
            }
        }
    }
}
